package chapter12Lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 仓库
 * Created by LinGer on 2019/6/19
 */
public class Depot {

    /**
     * 独占锁
     */
    private Lock lock;

    /**
     * 仓库的现有产品数量
     */
    private int size;

    public Depot(){
        //默认创建非公平锁
        this.lock = new ReentrantLock();
        this.size = 0;
    }

    public void produce(int val){
        //获取锁
//        lock.lock();
//        try {
            size += val;
            System.out.println(Thread.currentThread().getName() + " produce: " + val + " --> size=" + size);
//        }finally {
//            //释放锁
//            lock.unlock();
//        }
    }

    public void consume(int val){
//        lock.lock();
//        try{
            size -= val;
            System.out.println(Thread.currentThread().getName() + " consume: " + val + " <-- size=" + size);

//        } finally {
//            lock.unlock();
//        }
    }
}
